package com.my_musc_service.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my_musc_service.entity.LoginInfoVO;
import com.my_musc_service.entity.LoginVO;
import com.my_musc_service.entity.User;
import com.my_musc_service.exception.CustomException;
import com.my_musc_service.interceptor.LoginInterceptor;
import com.my_musc_service.service.LoginService;
import com.my_musc_service.service.UserService;
import com.my_musc_service.utils.JwtUtils;
import com.my_musc_service.utils.MD5;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author zhudunfeng
 * @date 2021/6/10 20:50
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserService userService;

    @Override
    public Map<String, Object> login(LoginVO loginVO) {

        String username = loginVO.getUsername();
        String password = loginVO.getPassword();
        //校验参数
        if (StringUtils.isEmpty(username)
                || StringUtils.isEmpty(password)) {
            throw new CustomException("用户名不存在或密码错误");
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name", username);
        User user = userService.getOne(userQueryWrapper);
//        User user = userDao.queryByUsername(username);
        if (user == null) {
            throw new CustomException("用户名不存在");
        }
        if (user.getSalt() != null) {
            password = DigestUtils.md5Hex(password + user.getSalt());
            if (!password.equals(user.getPassword())) {
//        if(!password.equals(user.getPassword())){
                throw new CustomException("密码错误");
            }
        } else {
            password = MD5.encrypt(password);
            if (!password.equals(user.getPassword())) {
                throw new CustomException("密码错误");
            }
        }

        String headerImgUrl = user.getHeaderImgUrl();
        //生成JWT加密的token
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("nickname", user.getNickName());
        claims.put("username", user.getUserName());
        claims.put("sex", user.getSex());
        claims.put("phone", user.getPhone());
        claims.put("headerImgUrl",headerImgUrl);

        String jwtToken = JwtUtils.generateJWT(claims);


        Map<String, Object> map = new HashMap<>();
        map.put("username", user.getUserName());
        map.put("userid", user.getId());
        map.put("headerImgUrl", headerImgUrl);
        map.put("token", jwtToken);


        return map;
    }

    @Override
    public LoginInfoVO getLoginInfoByJwtToken(String jwtToken) {
        //有效载荷
        User userInfo = LoginInterceptor.getUserInfo();
        LoginInfoVO loginInfoVO = new LoginInfoVO();
        if (userInfo != null) {
            BeanUtils.copyProperties(userInfo, loginInfoVO);
            User userData = userService.getOne(new QueryWrapper<User>().eq("id", userInfo.getId()));
            String headerImgUrl = Optional.ofNullable(userData).map(User::getHeaderImgUrl).orElse(null);
            if (StringUtils.isNotBlank(headerImgUrl)) {
                loginInfoVO.setHeaderImgUrl(headerImgUrl);
            }
        } else {
            throw new CustomException("解析用户信息失败");
        }
        return loginInfoVO;
    }
}
